<template>
	<view>
		<my-search @click="goSearch()"></my-search>
		<view class="scroll-view-container">
			<scroll-view class="left-scroll-view" scroll-y :style="{height: wh + 'px'}">
				<view class="scroll-item" :class="{active: item.cat_id == active}" v-for="(item,index) in cateList"
					:key="index" @click="lv1Change(item)">
					{{item.cat_name}}
				</view>
			</scroll-view>
			<scroll-view class="right-scroll-view" scroll-y :style="{height: wh + 'px'}" :scroll-top="scrollTop">
				<view class="scroll-item" v-for="(item,index) in productList" :key="index">
					<view class="cate-lv2-title">/ {{item.cat_name}} /</view>
					<view class="cate-lv3-box">
						<view class="cate-lv3-item" v-for="lv3 in item.children" :key="lv3.cat_id"
							@click="goGoodsList(lv3)">
							<image :src="lv3.cat_icon"></image>
							<text>{{lv3.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import badgeMix from '@/mixins/tabbar-badge.js'
	import {
		mySearch
	} from '../../components/my-search/my-search.vue'

	export default {
		mixins: [badgeMix],
		data() {
			return {
				active: 0,
				wh: 0,
				cateList: [],
				productList: [],
				scrollTop: 0
			};
		},
		components: {
			mySearch
		},
		onLoad() {
			const sysInfo = uni.getSystemInfoSync()
			this.wh = sysInfo.windowHeight - 50;
			this.getCateList();
		},
		methods: {
			goSearch(){
				uni.navigateTo({
					url: '/subPackages/search/search'
				})
			},
			goGoodsList(data) {
				uni.navigateTo({
					url: '/subPackages/goods_list/goods_list?cid=' + data.cat_id
				})
			},
			lv1Change(data) {
				this.active = data.cat_id;
				this.getProductList();
				this.scrollTop = this.scrollTop ? 0 : 1;
			},
			async getCateList() {
				const {
					data: res
				} = await uni.$http.get("/api/public/v1/categories");
				if (res.meta.status !== 200) {
					return uni.$showMsg()
				}
				this.cateList = res.message;
				this.active = this.cateList[0].cat_id;
				this.getProductList()
			},
			getProductList() {
				this.productList = this.cateList.filter(temp => temp.cat_id == this.active)[0].children
			}
		}
	}
</script>

<style lang="scss" scoped>
	.scroll-view-container {
		display: flex;

		.left-scroll-view {
			width: 120px;

			.scroll-item {
				background-color: #f7f7f7;
				line-height: 60px;
				text-align: center;
				font-size: 12px;

				&.active {
					background-color: white;
					position: relative;

					&::before {
						content: ' ';
						display: block;
						width: 3px;
						height: 30px;
						background-color: #c00000;
						position: absolute;
						left: 0;
						top: 50%;
						transform: translateY(-50%);
					}
				}
			}
		}

		.right-scroll-view {
			.scroll-item {
				.cate-lv2-title {
					font-size: 12px;
					font-weight: bold;
					text-align: center;
					padding: 15px 0;
				}

				.cate-lv3-box {
					display: flex;
					flex-wrap: wrap;

					.cate-lv3-item {
						width: 33%;
						display: flex;
						flex-direction: column;
						align-items: center;

						image {
							width: 60px;
							height: 60px;
						}

						text {
							font-size: 12px;
						}
					}
				}
			}
		}
	}
</style>
